<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检查描述" prop="checkrecordDesc">
        <el-input
          v-model="queryParams.checkrecordDesc"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查结果" prop="checkrecordResult">
        <el-input
          v-model="queryParams.checkrecordResult"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="选择检查状态" prop="checkrecordStatus">
        <el-select v-model="queryParams.checkrecordStatus" placeholder="请选择">
          <el-option label="请缴费" value="请缴费"></el-option>
          <el-option label="未检查" value="未检查"></el-option>
          <el-option label="已检查" value="已检查"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="检查时间" prop="chenckTime">
        <el-date-picker clearable
                        v-model="queryParams.chenckTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检查名称" prop="checkName">
        <el-input
          v-model="queryParams.checkName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医生名称" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病人姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddYan"
          v-hasPermi="['outpatient:checkrecord:add']"
        >新增
        </el-button>
      </el-col>
      <!--
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['outpatient:checkrecord:edit']"
            >检查完成</el-button>
          </el-col>


      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['outpatient:checkrecord:remove']"
        >删除
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['outpatient:checkrecord:export']"
        >导出
        </el-button>
      </el-col>
      -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkrecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="检查流水序号" align="center" prop="checkrecordId"/>
      <el-table-column label="检查问题描述" align="center" prop="checkrecordDesc"/>
      <el-table-column label="检查结果描述" align="center" prop="checkrecordResult"/>
      <el-table-column label="检查状态" align="center" prop="checkrecordStatus"/>

      <el-table-column label="检查时间" align="center" prop="chenckTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chenckTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检查类型" align="center" prop="checkName"/>
      <el-table-column label="检查医生" align="center" prop="doctorName"/>
      <el-table-column label="病人名称" align="center" prop="patientName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['outpatient:checkrecord:edit']"
          >修改检查状态
          </el-button>
         <!--
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['outpatient:checkrecord:remove']"
          >删除
          </el-button>
          -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getListYan"
    />

    <!-- 修改检查流水表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题描述" prop="checkrecordDesc">
          <el-input :value="form.checkrecordDesc"/>
        </el-form-item>
        <el-form-item label="检查结果描述" prop="checkrecordResult">
          <el-input v-model="form.checkrecordResult" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="检查状态" prop="checkrecordStatus">
          <el-select v-model="form.checkrecordStatus" placeholder="请选择">
            <el-option label="请缴费" value="请缴费"></el-option>
            <el-option label="未检查" value="未检查"></el-option>
            <el-option label="已检查" value="已检查"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查时间" prop="chenckTime">
          <el-date-picker clearable
                          :value="form.chenckTime"
                          type="date"
                          value-format="yyyy-MM-dd"
          >
            <!--
placeholder="请选择${comment}"
-->

          </el-date-picker>
        </el-form-item>

        <el-form-item label="检查名称" prop="checkId">
          <el-input :value="form.checkName"/>
        </el-form-item>
        <el-form-item label="医生名称" prop="doctorId">
          <el-input :value="form.doctorName"/>
        </el-form-item>

        <el-form-item label="病人名称" prop="registrationtypeId">
          <el-input :value="form.patientName"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加检查流水表对话框 -->
    <el-dialog :title="title" :visible.sync="openYan" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题描述" prop="checkrecordDesc">
          <el-input v-model="form.checkrecordDesc"/>
        </el-form-item>

        <el-form-item label="检查名称" prop="checkId">
          <el-select v-model="form.checkId" placeholder="请选择类型">
            <el-option
              v-for="item in formYan"
              :label="item.checkName"
              :value="item.checkId"
              @change="checksChange(checkId)"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="医生名称" prop="doctorId">
          <el-select v-model="form.doctorId" placeholder="请选择医生">
            <el-option
              v-for="item in doctorNameYan"
              :label="item.doctorName"
              :value="item.doctorId"
              @change="doctorChange(doctorId)"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="患者名称" prop="doctorId">
        <el-select
          v-model="form.patientId"
          placeholder="请选择患者名字"
          style="width: 100%"
        >
          <el-option
            v-for="item in patientList"
            :key="item.patientId"
            :label="item.patientName"
            :value="item.patientId"
          >
          </el-option>
        </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDPCListYan,
  getDPCList,
  updateCheckrecordYans,
  getCheckrecordYan,
  getCheckrecord,
  delCheckrecord,
  addCheckrecord,
  updateCheckrecord,
  showAll,
  addCheckrecordYan
} from '@/api/outpatient/checkrecord';

import {listPatient} from "@/api/user/patient";

export default {
  name: 'Checkrecord',
  data() {
    return {
      checkId: null,
      doctorId: null,
      // 遮罩层
      loading: true,
      // 所有患者信息
      patientList:[],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查流水表表格数据
      checkrecordList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openYan: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkrecordDesc: null,
        checkrecordResult: null,
        checkrecordStatus: null,
        chenckTime: null,
        checkId: null,
        checkName: null,
        doctorId: null,
        doctorName: null,
        patientId: null,
        patientName: null
      },
      // 表单参数
      form: {},
      formYan: [],
      doctorNameYan: [],
      add: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getListYan();
    this.getPatientList();
    this.handleAddYan();
  },
  methods: {
    /** 查询检查流水表列表 */
    getList() {
      this.loading = true
      showAll(this.queryParams).then(response => {
        this.checkrecordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询客户病历表列表 */
    getPatientList() {
      this.loading = true;
      listPatient(this.queryParams).then(response => {
        this.patientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    checksChange(checkId) {
      this.from.checkId = checkId
    },
    doctorChange(doctorId) {
      this.from.doctorId = doctorId
    },
    getListYan() {
      this.loading = true
      showAll(this.queryParams).then(response => {
        this.checkrecordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
        this.openYan = false,
        this.open = false
        this.doctorId = null,
        this.checkId = null,
        this.reset()
    },
    // 表单重置

    reset() {
      this.form = {
        checkrecordId: null,
        checkrecordDesc: null,
        checkrecordResult: null,
        checkrecordStatus: null,
        chenckTime: null,
        checkId: null,
        checkName: null,
        doctorId: null,
        doctorName: null,
        registrationtypeId: null,
        patientName: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getListYan()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkrecordId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加检查流水表'
    },

    getDoctorName() {
      getDPCListYan().then(response => {
        this.doctorNameYan = response.data.data
      })
    },
    getDPCName() {
      getDPCList().then(response => {
        this.formYan = response.rows
        console.log(this.formYan)
      })
    },

    handleAddYan() {
      this.reset()
      this.getDoctorName()
      this.getDPCName()
      this.openYan = true
      this.title = '添加检查流水表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const checkrecordId = row.checkrecordId || this.ids
      getCheckrecordYan(checkrecordId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改检查流水表'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.checkrecordId != null) {
            updateCheckrecordYans(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.openYan = false
              this.open = false
              this.getListYan()
            })
          } else {
            addCheckrecordYan(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.openYan = false
              this.open = false
              this.getListYan()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkrecordIds = row.checkrecordId || this.ids
      this.$modal.confirm('是否确认删除检查流水表编号为"' + checkrecordIds + '"的数据项？').then(function() {
        return delCheckrecord(checkrecordIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('outpatient/checkrecord/export', {
        ...this.queryParams
      }, `checkrecord_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
